package com.nondo.rdp.finance.domain.course;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.core.domain.attach.Attach;
import com.nondo.rdp.core.domain.base.BaseEntity;
import com.nondo.rdp.finance.config.DbConfig;

/**
 * 求知-课程-课节
 */
@Entity
@Table(name = DbConfig.DEFAULT_TABLE_PREFIX + "section", indexes = {@Index(columnList = "type,name")})
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Section extends BaseEntity {
	private static final long serialVersionUID = -1537742977391157982L;
	private Long id;// 主键
	private Course course;// 课程
	private String name;// 课节标题
	private Attach mainImg;// 视频封面
	private User publisher;// 发布人
	private Integer type;// 0：音频图文；1：视频；
	private Attach attach;// 音频或者视频
	private String content;// 图文或视频简介，
	private Date publishDate;// 发布时间 yyyy-MM-dd HH:mm:ss
	private int readNum;// 阅读数
	private int sortNum;// 排序值
	
	//扩展字段
	private Long courseId; // 课程ID
	private String orderBy; //排序方向 升=ASC,降=DESC
	private Integer albumId;//采集中的专辑ID
	private Integer trackId;//课节目唯一标示
	private String videoUrl;//视频地址，来源于采集，不需要上传attach

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@NotNull(message = "课节标题不能为空")
	@Length(min = 1, max = 32, message = "课节标题长度不能超过32个字符")
	@Column(name = "name", length = 32)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "main_img")
	public Attach getMainImg() {
		return mainImg;
	}

	public void setMainImg(Attach mainImg) {
		this.mainImg = mainImg;
	}

	@JsonIgnore
	@ManyToOne
	// 　--->
	// ManyToOne指定了多对一的关系，fetch=FetchType.LAZY属性表示在多的那一方通过延迟加载的方式加载对象(默认不是延迟加载)
	@JoinColumn(name = "cid")
	// 　--->　　通过 JoinColumn 的name属性指定了外键的名称
	public Course getCourse() {
		return course;
	}

	@JsonSetter
	public void setCourse(Course course) {
		this.course = course;
	}

	@OneToOne
	@JoinColumn(name = "uid")
	public User getPublisher() {
		return publisher;
	}

	public void setPublisher(User publisher) {
		this.publisher = publisher;
	}

	@NotNull(message = "类型不能为空")
	@Column(columnDefinition = "int default 0", nullable = false)
	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "attach_id")
	public Attach getAttach() {
		return attach;
	}

	public void setAttach(Attach attach) {
		this.attach = attach;
	}

	@Lob
	@Column(columnDefinition="TEXT")
	@NotNull(message = "课节内容不能为空")
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}

	@Column(columnDefinition = "int default 0", nullable = false)
	public int getReadNum() {
		return readNum;
	}

	public void setReadNum(int readNum) {
		this.readNum = readNum;
	}
	
	@Column(columnDefinition = "int default 0", nullable = false)
	public int getSortNum() {
		return sortNum;
	}

	public void setSortNum(int sort) {
		this.sortNum = sort;
	}

	@JsonIgnore
	@Transient
	public Long getCourseId() {
		return courseId;
	}

	@JsonSetter
	public void setCourseId(Long courseId) {
		this.courseId = courseId;
	}

	@JsonIgnore
	@Transient
	public String getOrderBy() {
		return orderBy;
	}

	@JsonSetter
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getVideoUrl() {
		return videoUrl;
	}

	public void setVideoUrl(String videoUrl) {
		this.videoUrl = videoUrl;
	}

	public Integer getAlbumId() {
		return albumId;
	}

	public void setAlbumId(Integer albumId) {
		this.albumId = albumId;
	}

	public Integer getTrackId() {
		return trackId;
	}

	public void setTrackId(Integer trackId) {
		this.trackId = trackId;
	}
}
